package org.begincode.web.control.resource;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.begincode.core.model.Resource;
import org.begincode.core.paginator.domain.PageList;
import org.begincode.core.paginator.domain.Paginator;
import org.begincode.exception.BeginCodeException;
import org.begincode.resource.service.ResourceService;
import org.begincode.resource.service.ResourceTypeService;
import org.begincode.task.queue.CountOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

@Controller
@RequestMapping({"/resource"})
public class ResourceController {
    private Logger logger = Logger.getLogger(ResourceController.class);

    @Autowired
    ResourceService resourceService;

    @Autowired
    ResourceTypeService resourceTypeService;

    @Autowired
    CountOperator countOperator;

    @RequestMapping(value = {""}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    public String getResourceList(Model model) {
        Paginator page = new Paginator(0, 10);
        page.setOrderStr(" order by create_datetime desc ");
        PageList list = this.resourceService.findResources(page);
        model.addAttribute("resources", list);
        model.addAttribute("pageinfo", list.getPaginator());
        return "/page/resource/resource_list";
    }

    @RequestMapping(value = {"/resources"}, method = {RequestMethod.GET})
    @ResponseBody
    public List getResources(Paginator pageinfo) {
        if (pageinfo != null) {
            pageinfo.setLimit(10);
            pageinfo.setOrderStr(" order by create_datetime desc ");
            PageList list = this.resourceService.findResources(pageinfo);
            return list;
        }
        return null;
    }

    @RequestMapping(value = {"/fortype/{typeId}"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    public String getResourcesByType(@PathVariable("typeId") int typeId, Paginator pageinfo, Model model) {
        pageinfo.setOrderStr(" order by create_datetime desc ");
        PageList list = getResourceByType(typeId, pageinfo);
        model.addAttribute("resources", list);
        model.addAttribute("pageinfo", list.getPaginator());
        model.addAttribute("resourceType", Integer.valueOf(typeId));
        return "/page/resource/resource_for_type_list";
    }

    @RequestMapping(value = {"/resources/type/{typeId}"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List getResourcesByType(@PathVariable("typeId") int typeId, Paginator pageinfo) {
        return getResourceByType(typeId, pageinfo);
    }

    public PageList getResourceByType(int typeId, Paginator pageinfo) {
        Resource resource = new Resource();
        resource.setResourceTypeId(Integer.valueOf(typeId));
        pageinfo.setOrderStr(" order by create_datetime desc ");
        if ((pageinfo == null) || (pageinfo.getLimit() <= 0)) {
            pageinfo.setLimit(10);
        }

        PageList list = this.resourceService.findResourcesByRecords(resource, pageinfo);
        return list;
    }

    @RequestMapping(value = {"/top/{size}"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List findDateTop(@PathVariable("size") int size) {
        return findTopByDate(size);
    }

    @RequestMapping(value = {"/top/{size}/count"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List findCountTop(@PathVariable("size") int size) {
        return findTopByCount(size);
    }

    private List findTopByDate(int n) {
        if (n <= 0) {
            n = 10;
        }
        Paginator pageinfo = new Paginator(0, n);
        pageinfo.setOrderStr(" order by create_datetime desc ");
        PageList list = this.resourceService.findResources(pageinfo);
        return list;
    }

    private List findTopByCount(int n) {
        if (n <= 0) {
            n = 10;
        }
        Paginator pageinfo = new Paginator(0, n);
        pageinfo.setOrderStr(" order by down_count desc ");
        PageList list = this.resourceService.findResources(pageinfo);
        return list;
    }

    @RequestMapping(value = {"/{resourceId}"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    public String selResources(Model model, @PathVariable("resourceId") int resourceId) {
        Resource record = this.resourceService.findResourceById(resourceId);
        if (record == null) {
            this.logger.info("resourceId 不存在");
            throw new BeginCodeException("resourceId 不存在" + resourceId);
        }

        countOperator.addResourceViewCount(resourceId);
        model.addAttribute("resource", record);
        return "/page/resource/resource_view";
    }



    @RequestMapping(value = {"/resource/{size}/type/{typeId}"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List findRelationTop(@PathVariable("typeId") int typeId, @PathVariable("size") int size) {
        Paginator pageinfo = new Paginator(0, size);
        pageinfo.setOrderStr(" order by down_count desc ");
        Resource record = new Resource();
        record.setResourceTypeId(Integer.valueOf(typeId));
        PageList list = this.resourceService.findResourcesByRecords(record, pageinfo);
        return list;
    }

    @RequestMapping(value = {"/resourceType"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List findResourceType() {
        return this.resourceTypeService.findResourceType();
    }


}